﻿using UnityEngine;
using System.Collections;
using LitJson;
using System;
using Jhqc.EditorCommon;
using SimpleJSON;
using System.Collections.Generic;
using NewEditorNS;

/// <summary>场景物体 作为CompVO和SceneNodeMono的基类</summary>
public class SceneObj : MonoBehaviour, IJsonData
{
    #region data
    public virtual IJsonData ReadJsonData(JsonData jd)
    {
        throw new NotImplementedException();
    }

    public virtual JsonData ToJsonData()
    {
        throw new NotImplementedException();
    }

    #endregion

    #region 模型显示

    bool showModel = false;
    public virtual bool ShowModel
    {
        get
        {
            return showModel;
        }

        set
        {
            bool oldValue = showModel;
            showModel = value;

            if (!showModel)
                disposeModel();
            else if (theModelState == EnumModelState.empty)
                loadModel(createModel, createBrokenModel);
        }
    }

    public virtual void SetModelVisiblePyCameraPos(Vector3 camPos)
    {

    }

    #endregion

    #region 模型加载与卸载

    EnumModelState theModelState = EnumModelState.empty;

    GameObject modelContent;

    protected virtual void loadModel(Action onLoaded, Action<HttpResp.ErrorType, string> onLoadFailed)
    {
        theModelState = EnumModelState.creating;
    }

    protected virtual void createModel()
    {
        //当createModel作为加载完成后的回调执行时, 若已经不在相机范围, 则不创建模型
        if (!ShowModel)
        {
            theModelState = EnumModelState.empty;
            return;
        }
        if (modelContent != null)
            Destroy(modelContent);

        //other code

        //do this after create model
        //theModelState = EnumModelState.created;
    }

    /// <summary>创建加载失败的模型</summary>
    protected virtual void createBrokenModel(HttpResp.ErrorType errorType, string errorInfo)
    {
    }


    protected virtual void disposeModel()
    {
        if (theModelState == EnumModelState.created)
        {
            if (modelContent != null)
                Destroy(modelContent);
        }
        theModelState = EnumModelState.empty;
    }

    #endregion
}
